#include "hw_ints.h"
#include "hw_memmap.h"
#include "hw_types.h"
#include "adc.h"
#include "debug.h"
#include "gpio.h"
#include "interrupt.h"
#include "sysctl.h"
#include "timer.h"
#include "uart.h"
#include "..\osram96x16x1.h"
#include "stdlib.h"
#include "uart_clock.h"
#include "uart_menu.h"


#define PUSH_BUTTON		GPIO_PIN_4		// Button pin
#define USER_LED		GPIO_PIN_5		// Led pin

int button_flag = 0;

// The error routine that is called if the driver library encounters an error.
//
#ifdef DEBUG
void
__error__(char *pcFilename, unsigned long ulLine)
{
}
#endif

void
delay(unsigned long long a) {
	while(--a);
}
////////////////////////////////////////////////////////////////



//////////////////////////////////////////////////////////////////

int main(void)
{
	
	unsigned long ticks, enter_flag = 1;
//	unsigned int fl;

    // Set the clocking to run directly from the crystal.
	//
    SysCtlClockSet(SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
                   SYSCTL_XTAL_6MHZ);
	
	IntMasterDisable();
    

    // Initialize the OLED display and write status.
    //
    OSRAM96x16x1Init(true);
	OSRAM96x16x1StringDraw("Connected to PC", 0, 0);
	OSRAM96x16x1StringDraw("Mode:        OFF", 0, 1);

	//
    // Enable the peripherals used by the application.
    //
	
    SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
	SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1);
	SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC); 
	//Enable the UART
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
	
	//
    // Configure the ADC to sample the potentiometer when the timer expires.
    //
    ADCSequenceConfigure(ADC_BASE, 0, ADC_TRIGGER_PROCESSOR, 0);
    ADCSequenceStepConfigure(ADC_BASE, 0, 0, ADC_CTL_CH0 |  ADC_CTL_IE | ADC_CTL_END);
    ADCSequenceEnable(ADC_BASE, 0);


	// Configure the push button as required.
    //
    GPIOPinTypeGPIOInput(GPIO_PORTC_BASE, PUSH_BUTTON);
	

    // Set GPIO A0 and A1 as UART pins.
    //
    GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);


	// Configure the UART for 115,200, 8-N-1 operation.
    //
    UARTConfigSetExpClk(UART0_BASE, SysCtlClockGet(), 115200,
                        (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
                         UART_CONFIG_PAR_NONE));


	// Configure one 32-bit periodic timers. And one OS timer in 16 bit mode.
    //
	TimerConfigure(TIMER0_BASE, TIMER_CFG_32_BIT_PER);
	ticks = SysCtlClockGet();
    TimerLoadSet(TIMER0_BASE, TIMER_A, ticks);

	TimerConfigure(TIMER1_BASE, TIMER_CFG_32_BIT_OS);
	TimerLoadSet(TIMER1_BASE, TIMER_A, SysCtlClockGet()/2);

	// Setup the interrupts for the timer timeouts
	//
    IntEnable(INT_TIMER0A);
	IntEnable(INT_TIMER1A);
	IntEnable(INT_GPIOC);
	GPIOPinIntEnable(GPIO_PORTC_BASE, GPIO_PIN_4);
	TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
	TimerIntEnable(TIMER1_BASE, TIMER_TIMA_TIMEOUT);
	


	// Enable the timers.
    //
    TimerEnable(TIMER0_BASE, TIMER_A);


	// Enable processor interrupts.
    //
    IntMasterEnable();
	
	//
	// Loop forever while the timers run.
    //
	for(;;) {
		
		if (enter_flag)
		{
			IntMasterDisable();
			EnterTime();
			ProgramsTimesInput();
			enter_flag = 0;
			IntMasterEnable();
		}
		if (!button_flag) continue;  
		else
		{ 
			OSRAM96x16x1StringDraw("OFF", 78, 1);
			TimerDisable(TIMER0_BASE, TIMER_A);
			button_flag = 0;
			IntMasterDisable();
			OSRAM96x16x1StringDraw("                ",0,0);
			IntMasterEnable();
			GetDataFromACP();
			TimerEnable(TIMER0_BASE, TIMER_A);
		}	
	}		
}



